package com.zhongwang.business.config.timingtask;

import com.zhongwang.business.module.estimateBudgetAccountingManage.service.SynchronizeDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.batch.BatchProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * @author 徐一贺
 * @className SynchronizeDataTask
 * @description 定时同步数据任务
 * @date 2020/12/16 18:17
 * @versron ver 1.0
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling // 2.开启定时任务
@Component
public class SynchronizeDataTask implements SchedulingConfigurer {
    @Autowired
    private SynchronizeDataService service;
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        Method[] methods = BatchProperties.Job.class.getMethods();
        int defaultPoolSize = 3;
        int corePoolSize = 0;
        if (methods != null && methods.length > 0) {
            for (Method method : methods) {
                Scheduled annotation = method.getAnnotation(Scheduled.class);
                if (annotation != null) {
                    corePoolSize++;
                }
            }
            if (defaultPoolSize > corePoolSize) {
                corePoolSize = defaultPoolSize;
            }
        }
        taskRegistrar.setScheduler(new ScheduledThreadPoolExecutor(corePoolSize));
    }

    /**  monthStatisticalData 每分钟执行一次
     * @param
     * @return void
     * @exception
     * @author 徐一贺
     * @date 2020/12/16 18:16
     */
    @Scheduled(cron = "0 0/3 * * * ?")
    private void monthStatisticalData() {
//        service.synchronizeData();
    }
}
 